package com.sunday.electric.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sunday.common.widgets.EmptyLayout;
import com.sunday.electric.R;
import com.sunday.electric.base.BaseFragment;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;
import org.xwalk.core.XWalkWebResourceRequest;
import org.xwalk.core.XWalkWebResourceResponse;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/1/9.
 */
public class IndexFragment extends BaseFragment {
    @Bind(R.id.webView)
    XWalkView mXWalkView;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    private String indexUrl =  "http://www.bbaba.com:7080/HD_JC/app/appPage.go?method=goHomePage";
    private String plainUrl =  "http://www.bbaba.com:7080/HD_JC/app/appPage.go?method=goPlanListPage";
    private String thirdUrl =  "http://www.bbaba.com:7080/HD_JC/app/appPage.go?method=goJcPlanListPage";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_index,null);
        ButterKnife.bind(this,rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        WebSettings settings = webView.getSettings();
//        settings.setSupportZoom(true);          //支持缩放
//        settings.setBuiltInZoomControls(true);  //启用内置缩放装置
//        settings.setJavaScriptEnabled(true);    //启用JS脚本
        mXWalkView.load(indexUrl,null);
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        mXWalkView.setUIClient(new MyUIClient(mXWalkView));

        mXWalkView.setResourceClient(new XWalkResourceClient(mXWalkView){

            @Override
            public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                super.onReceivedLoadError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onLoadFinished(XWalkView view, String url) {
                emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                super.onLoadFinished(view, url);
            }

            @Override
            public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
                return super.shouldInterceptLoadRequest(view, request);
            }

            @Override
            public void onLoadStarted(XWalkView view, String url) {
                super.onLoadStarted(view, url);
            }
        });
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mXWalkView.load(indexUrl,null);
            }
        });
    }
    class MyUIClient extends XWalkUIClient {
        MyUIClient(XWalkView view) {
            super(view);
        }

        @Override
        public void onFullscreenToggled(XWalkView view, boolean enterFullscreen) {
            super.onFullscreenToggled(view, enterFullscreen);
        }
    }

    public void loadIndex(){
        mXWalkView.load(plainUrl,null);
    }
    public void loadPlain(){
        mXWalkView.load(plainUrl,null);
    }
    public void loadThird(){
        mXWalkView.load(thirdUrl,null);
    }
    public void clearCache(){
       // mXWalkView.get
    }

}
